//
//  SuperToursimVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "SuperToursimVC.h"
#import "SDCycleScrollView.h"
#import "SuperToursimModel.h"
#import "SuperToursimCell.h"
#import "SuperToursimPlaceVC.h"

@interface SuperToursimVC ()<UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate,MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) MONActivityIndicatorView *indicationView;


@end

@implementation SuperToursimVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor purpleColor];
    [self addSubViews];
    // 隐藏分割线
    self.tableView.separatorStyle = NO;
    
    // 加载一个等待动画
    self.indicationView = [[MONActivityIndicatorView alloc] init];
    self.indicationView.center = self.tableView.center;
    CGPoint point = self.indicationView.center;
    point.y = self.tableView.center.y - 200;
    self.indicationView.center = point;
    self.indicationView.delegate = self;
    [self.tableView addSubview:self.indicationView];
    [self.indicationView startAnimating];
    
    [self UpData];
}

// 实现等待图片的加载动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    if (index == 0) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 1) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 2) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 3) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    }
    
    return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}



// 加载视图
- (void)addSubViews
{
    NSArray *array = [NSArray array];
    UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"2.jpg"];
    UIImage *image3 = [UIImage imageNamed:@"3.jpg"];
    UIImage *image4 = [UIImage imageNamed:@"4.jpg"];
    
    
    array = @[image1,image2,image3,image4];
     SDCycleScrollView *scrollViewss = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KsWidth, 244) imagesGroup:array];
    scrollViewss.delegate = self;
    
    [self.view addSubview:scrollViewss];
    
       // 表的创建
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,  244, KsWidth, KsHeigh - 244) style:(UITableViewStylePlain)];
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    self.navigationItem.title = @"专题推荐";
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"sfggfesfdff";
    SuperToursimCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SuperToursimCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    SuperToursimModel *model = self.dataArray[indexPath.row];
    NSString *string = model.img;
    [cell.imagesView sd_setImageWithURL:[NSURL URLWithString:string]];
    cell.titleLabel.text = model.name;
    cell.descLabel.text = model.desc;
    // 取消加载动画
    [self.indicationView stopAnimating];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 传送mtid到下一个页面
    SuperToursimModel *model = self.dataArray[indexPath.row];
    SuperToursimPlaceVC *placeVC = [[SuperToursimPlaceVC alloc] init];
    placeVC.mtids = model.mtid;
    placeVC.tabTitle = model.name;
    [self.navigationController pushViewController:placeVC animated:YES];
}



#pragma mark ======  获取scrollview 的index的代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if (index == 0) {
        // 实现对数据的加载
        NSLog(@"点击的是第1页");
        SuperToursimPlaceVC *placeVC = [[SuperToursimPlaceVC alloc] init];
        placeVC.mtids = @"1";
        placeVC.tabTitle = @"春天专区";
        [self.navigationController pushViewController:placeVC animated:YES];
        
        
    } else if (index == 1)
    {
        NSLog(@"点击的是第2页");
        // 实现对夏天数据的加载
        SuperToursimPlaceVC *placeVC = [[SuperToursimPlaceVC alloc] init];
        placeVC.mtids = @"2";
        placeVC.tabTitle = @"夏季专区";
        [self.navigationController pushViewController:placeVC animated:YES];
    } else if (index == 2) {
        NSLog(@"点击的是第3页");
        // 实现对秋天类型的数据的加载
        SuperToursimPlaceVC *placeVC = [[SuperToursimPlaceVC alloc] init];
        placeVC.mtids = @"3";
        placeVC.tabTitle = @"秋季专区";
        [self.navigationController pushViewController:placeVC animated:YES];
    } else if (index == 3) {
        NSLog(@"点击的是第4页");
        // 实现对冬天数据的加载
        SuperToursimPlaceVC *placeVC = [[SuperToursimPlaceVC alloc] init];
        placeVC.mtids = @"4";
        placeVC.tabTitle = @"冬天专区";
        [self.navigationController pushViewController:placeVC animated:YES];
    }
}

- (void)UpData
{
//    NSString *strURL = @"http://www.roadqu.com/api/mobile/qunawan/tour/featuredthemeslist";
//    NSURL *url = [NSURL URLWithString:strURL];
//    // &a_t=1437446076000&token=55a60422f1df1&sign=facab2cd96a4f35b4cb0c89abb5a17c9e2009dc7
//    NSLog(@"==============%@",url);
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
    NSURL *url = [NSURL URLWithString:@"http://www.roadqu.com/api/mobile/qunawan/tour/featuredthemeslist"];
    //创建request网络请求对象
    NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    [mutableURLRequest setHTTPMethod:@"post"];
    //设置请求参数
    NSData *postData = [[NSString stringWithFormat:@"&a_t=1437446076000&token=55a60422f1df1&sign=facab2cd96a4f35b4cb0c89abb5a17c9e2009dc7"]dataUsingEncoding:NSUTF8StringEncoding];
    [mutableURLRequest setHTTPBody:postData];

    
    [NSURLConnection sendAsynchronousRequest:mutableURLRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        // 进行数据解析
        self.dataArray = [NSMutableArray array];
        
        if (data == nil) {
            return;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"data"];
        
        for (NSDictionary *dic1 in array) {
            // 初始化model
            SuperToursimModel *model = [[SuperToursimModel alloc] init];
            model.img = dic1[@"img"];
            model.name = dic1[@"name"];
            model.desc = dic1[@"desc"];
            model.mtid = dic1[@"mtid"];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
       
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
